<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
                    <el-form-item label="批次号" prop="batchNumber">
                      <el-input v-model="formData.batchNumber" placeholder="请输入批次号" />
                    </el-form-item>
                    <el-form-item label="物料编码" prop="materialCode">
                      <el-input v-model="formData.materialCode" placeholder="请输入物料编码" />
                    </el-form-item>
                    <el-form-item label="数量" prop="quantity">
                      <el-input v-model="formData.quantity" placeholder="请输入数量" />
                    </el-form-item>
                    <el-form-item label="单位" prop="unit">
                      <el-input v-model="formData.unit" placeholder="请输入单位" />
                    </el-form-item>
                    <el-form-item label="物料类型" prop="materialType">
                      <el-select v-model="formData.materialType" placeholder="请选择物料类型">
                            <el-option label="请选择字典生成" value="" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="物料状态" prop="materialState">
                      <el-input v-model="formData.materialState" placeholder="请输入物料状态" />
                    </el-form-item>
                    <el-form-item label="仓库id" prop="warehouseId">
                      <el-input v-model="formData.warehouseId" placeholder="请输入仓库id" />
                    </el-form-item>
                    <el-form-item label="库位id" prop="locationId">
                      <el-input v-model="formData.locationId" placeholder="请输入库位id" />
                    </el-form-item>
                    <el-form-item label="GRN编码" prop="grnCode">
                      <el-input v-model="formData.grnCode" placeholder="请输入GRN编码" />
                    </el-form-item>
                    <el-form-item label="加工属性" prop="processingAttributes">
                      <el-input v-model="formData.processingAttributes" placeholder="请输入加工属性" />
                    </el-form-item>
                    <el-form-item label="备注" prop="remark">
                      <el-input v-model="formData.remark" placeholder="请输入备注" />
                    </el-form-item>
                    <el-form-item label="炉号" prop="stoveCode">
                      <el-input v-model="formData.stoveCode" placeholder="请输入炉号" />
                    </el-form-item>
                    <el-form-item label="模号" prop="patternNumber">
                      <el-input v-model="formData.patternNumber" placeholder="请输入模号" />
                    </el-form-item>
                    <el-form-item label="生产计划号" prop="productionCode">
                      <el-input v-model="formData.productionCode" placeholder="请输入生产计划号" />
                    </el-form-item>
                    <el-form-item label="转码前追溯码" prop="frontRetrospectCode">
                      <el-input v-model="formData.frontRetrospectCode" placeholder="请输入转码前追溯码" />
                    </el-form-item>
                    <el-form-item label="追溯码" prop="retrospectCode">
                      <el-input v-model="formData.retrospectCode" placeholder="请输入追溯码" />
                    </el-form-item>
      </el-form>
              <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import * as MaterialApi from '@/api/storage/material';
      export default {
    name: "MaterialForm",
    components: {
                    },
    data() {
      return {
        // 弹出层标题
        dialogTitle: "",
        // 是否显示弹出层
        dialogVisible: false,
        // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
        formLoading: false,
        // 表单参数
        formData: {
                            id: undefined,
                            batchNumber: undefined,
                            materialCode: undefined,
                            quantity: undefined,
                            unit: undefined,
                            materialType: undefined,
                            materialState: undefined,
                            warehouseId: undefined,
                            locationId: undefined,
                            grnCode: undefined,
                            processingAttributes: undefined,
                            remark: undefined,
                            stoveCode: undefined,
                            patternNumber: undefined,
                            productionCode: undefined,
                            frontRetrospectCode: undefined,
                            retrospectCode: undefined,
        },
        // 表单校验
        formRules: {
                        batchNumber: [{ required: true, message: '批次号不能为空', trigger: 'blur' }],
                        materialCode: [{ required: true, message: '物料编码不能为空', trigger: 'blur' }],
        },
                        };
    },
    methods: {
      /** 打开弹窗 */
     async open(id) {
        this.dialogVisible = true;
        this.reset();
        // 修改时，设置数据
        if (id) {
          this.formLoading = true;
          try {
            const res = await MaterialApi.getMaterial(id);
            this.formData = res.data;
            this.title = "修改仓储物料表	";
          } finally {
            this.formLoading = false;
          }
        }
        this.title = "新增仓储物料表	";
              },
      /** 提交按钮 */
      async submitForm() {
        // 校验主表
        await this.$refs["formRef"].validate();
                  this.formLoading = true;
        try {
          const data = this.formData;
                  // 修改的提交
          if (data.id) {
            await MaterialApi.updateMaterial(data);
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.$emit('success');
            return;
          }
          // 添加的提交
          await MaterialApi.createMaterial(data);
          this.$modal.msgSuccess("新增成功");
          this.dialogVisible = false;
          this.$emit('success');
        } finally {
          this.formLoading = false;
        }
      },
                      /** 表单重置 */
      reset() {
        this.formData = {
                            id: undefined,
                            batchNumber: undefined,
                            materialCode: undefined,
                            quantity: undefined,
                            unit: undefined,
                            materialType: undefined,
                            materialState: undefined,
                            warehouseId: undefined,
                            locationId: undefined,
                            grnCode: undefined,
                            processingAttributes: undefined,
                            remark: undefined,
                            stoveCode: undefined,
                            patternNumber: undefined,
                            productionCode: undefined,
                            frontRetrospectCode: undefined,
                            retrospectCode: undefined,
        };
        this.resetForm("formRef");
      }
    }
  };
</script>